package com.pactera.madp.cp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.cp.api.dto.marketing.MarketingHotDTO;
import com.pactera.madp.cp.api.em.workcontact.EventEnum;
import com.pactera.madp.cp.api.entity.WorkEmployeeEntity;
import com.pactera.madp.cp.api.entity.marketing.*;
import com.pactera.madp.cp.api.vo.contact.ContactDetailVO;
import com.pactera.madp.cp.api.vo.marketing.MarketingHotVO;
import com.pactera.madp.cp.common.constant.BehaviorTrackFormatConst;
import com.pactera.madp.cp.common.util.RedisUtil;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.marketing.MarketingHotMapper;
import com.pactera.madp.cp.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * (CpMarketingHot)表服务实现类
 *
 * @author yushen.qi
 * @since 2021-07-01 15:22:10
 */
@Service
@Slf4j
public class MarketingHotServiceImpl extends ServiceImpl<MarketingHotMapper,MarketingHotEntity> implements MarketingHotService {

    @Autowired
    private  MarketingHotMapper marketingHotMapper;
    @Autowired
    private  MarketingArticleService marketingArticleService;
    @Autowired
    private  MarketingLinkService marketingLinkService;
    @Autowired
    private  MarketingPdfService marketingPdfService;
    @Autowired
    private  MarketingPptService marketingPptService;
    @Autowired
    private  MarketingWordService marketingWordService;
    @Autowired
    private  MarketingPosterService marketingPosterService;
    @Autowired
    private  MarketingExcelService marketingExcelService;
    @Autowired
    private IWorkEmployeeService workEmployeeService;
    @Autowired
    private IContactService contactService;

    @Override
    public void countOpen(String id, String type, String url, String wxExternalUserid, String wxUserId, String location, String title, Integer corpId, HttpServletResponse response) throws IOException {
        if (wxExternalUserid != null && wxUserId != null && location != null && title != null && corpId != null) {
            handleBehavior(wxExternalUserid,wxUserId,location,title,corpId,url);
        }

        Integer count = redisCuntOpen(id, type);
        //得到素材类型，1为文章，2为链接，3为海报，4为pdf，5为ppt，6为文档。
        Integer typeOne = Integer.valueOf(type);
        Integer idAll = Integer.valueOf(id);
        switch (typeOne) {
            case 1:
                //通过素材唯一id得到该素材类型中的记录。
                MarketingArticleEntity articleEntity = marketingArticleService.getOne(Wrappers.<MarketingArticleEntity>lambdaQuery()
                        .eq(MarketingArticleEntity::getId, idAll));
                //如果没找到该条记录存在，则跳出本次循环。
                if (null == articleEntity) {
                    break;
                }
                MarketingArticleEntity entity = new MarketingArticleEntity();
                //更新打开次数+1
                entity.setOpen(articleEntity.getOpen() + 1);
                marketingArticleService.update(entity, Wrappers.<MarketingArticleEntity>lambdaQuery().eq(MarketingArticleEntity::getId, idAll));
                log.info("url"+url);
                log.info("url"+URLDecoder.decode(url));
                String decode = URLDecoder.decode(url);
                response.sendRedirect(decode);
                break;
            case 2:
                MarketingLinkEntity link = marketingLinkService.getOne(Wrappers.<MarketingLinkEntity>lambdaQuery().eq(MarketingLinkEntity::getId, idAll));
                if (null == link) {
                    break;
                }
                MarketingLinkEntity linkEntity = new MarketingLinkEntity();
                linkEntity.setOpen(link.getOpen() + 1);
                marketingLinkService.update(linkEntity, Wrappers.<MarketingLinkEntity>lambdaQuery().eq(MarketingLinkEntity::getId,idAll));
                response.sendRedirect(URLDecoder.decode(url));
                break;
            case 3:
                MarketingPosterEntity posterEntity = marketingPosterService.getOne(Wrappers.<MarketingPosterEntity>lambdaQuery().eq(MarketingPosterEntity::getId, idAll));
                if (null == posterEntity) {
                    break;
                }
                MarketingPosterEntity poster = new MarketingPosterEntity();
                poster.setOpen(posterEntity.getOpen() + 1);
                marketingPosterService.update(poster, Wrappers.<MarketingPosterEntity>lambdaQuery().eq(MarketingPosterEntity::getId,idAll));
                response.sendRedirect(URLDecoder.decode(url));
                break;
            case 4:
                MarketingPdfEntity pdfEntity = marketingPdfService.getOne(Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getId, idAll));
                if (null == pdfEntity) {
                    break;
                }
                MarketingPdfEntity pdf = new MarketingPdfEntity();
                pdf.setOpen(pdfEntity.getOpen() + 1);
                marketingPdfService.update(pdf, Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getId,idAll));

                response.sendRedirect(URLDecoder.decode(url));
                break;
            case 5:
                MarketingPptEntity pptEntity = marketingPptService.getOne(Wrappers.<MarketingPptEntity>lambdaQuery().eq(MarketingPptEntity::getId, idAll));
                if (null == pptEntity) {
                    break;
                }
                MarketingPptEntity pptEntity1 = new MarketingPptEntity();
                pptEntity1.setOpen(pptEntity.getOpen() + 1);
                marketingPptService.update(pptEntity1, Wrappers.<MarketingPptEntity>lambdaQuery().eq(MarketingPptEntity::getId,idAll));
                response.sendRedirect(URLDecoder.decode(url));
                break;
            case 6:
                MarketingExcelEntity one = marketingExcelService.getOne(Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getId, idAll));
                MarketingExcelEntity excelEntity = new MarketingExcelEntity();
                excelEntity.setOpen(one.getOpen()+1);
                marketingExcelService.update(excelEntity,Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getId,idAll));
                response.sendRedirect(URLDecoder.decode(url));
                break;
            case 7:
                MarketingWordEntity wordEntity = marketingWordService.getOne(Wrappers.<MarketingWordEntity>lambdaQuery().eq(MarketingWordEntity::getId, idAll));
                if (null == wordEntity) {
                    break;
                }
                MarketingWordEntity wordEntity1 = new MarketingWordEntity();
                wordEntity1.setOpen(wordEntity.getOpen() + 1);
                marketingWordService.update(wordEntity1, Wrappers.<MarketingWordEntity>lambdaQuery().eq(MarketingWordEntity::getId, idAll));
                response.sendRedirect(URLDecoder.decode(url));
                break;
            default:
                break;
        }
    }

    private void handleBehavior(String wxExternalUserid, String wxUserId, String location, String title, Integer corpId, String url) {
        ContactDetailVO contact = contactService.getContactDetailByWxExternalUserId(wxExternalUserid);
        WorkEmployeeEntity employee = workEmployeeService.getOne(new LambdaQueryWrapper<WorkEmployeeEntity>()
                .eq(WorkEmployeeEntity::getCorpId, corpId)
                .eq(WorkEmployeeEntity::getWxUserId, wxUserId));
        List<String> datas = new ArrayList<>();
        datas.add(contact.getName());
        datas.add(employee.getName());
        datas.add(location);
        datas.add(url);
        datas.add(title);
        String content = String.format(BehaviorTrackFormatConst.MarketingTrackFormat, datas.toArray(new String[datas.size()]));
        //保存轨迹 channel:2-侧边栏
        contactService.saveTrack(employee.getId(), contact.getId(), EventEnum.MARKETING, content,2, corpId);
    }

    @Override
    public void countSend(String id, String type, String wxExternalUserid) {
        String corpId = String.valueOf(CorpContextHolder.getCorpId());
        String template= StrUtil.format("cp:marketing.send:{}",corpId+id + type + wxExternalUserid);
        //获取redis中唯一key，key由id，type，userId组成，确保唯一性。
        Object sendId = RedisUtil.get(template);
        if (sendId == null) {
            //创建该用户一天内发送次数唯一key-value。
            RedisUtil.set(template, 1, 24 * 60 * 60);
            //将type转换为整型
            Integer typeOne = Integer.valueOf(type);
            //将id转换为整型
            Integer idAll = Integer.valueOf(id);
            switch (typeOne) {
                case 1:
                    //在对应表通过id查找本条记录。
                    MarketingArticleEntity articleEntity = marketingArticleService.getOne(Wrappers.<MarketingArticleEntity>lambdaQuery()
                            .eq(MarketingArticleEntity::getId, idAll));
                    //如果没找到本条记录，则跳出本次循环。
                    if (null == articleEntity) {
                        break;
                    }
                    MarketingArticleEntity entity = new MarketingArticleEntity();
                    //如果找到，转发次数加1.
                    entity.setSend(articleEntity.getSend() + 1);
                    marketingArticleService.update(entity, Wrappers.<MarketingArticleEntity>lambdaQuery().eq(MarketingArticleEntity::getId, idAll));
                    break;
                case 2:
                    MarketingLinkEntity link = marketingLinkService.getOne(Wrappers.<MarketingLinkEntity>lambdaQuery().eq(MarketingLinkEntity::getId, idAll));
                    if (null == link) {
                        break;
                    }
                    MarketingLinkEntity linkEntity = new MarketingLinkEntity();
                    linkEntity.setSend(link.getSend() + 1);
                    marketingLinkService.update(linkEntity, Wrappers.<MarketingLinkEntity>lambdaQuery().eq(MarketingLinkEntity::getId, idAll));
                    break;
                case 3:
                    MarketingPosterEntity posterEntity = marketingPosterService.getOne(Wrappers.<MarketingPosterEntity>lambdaQuery().eq(MarketingPosterEntity::getId, idAll));
                    if (null == posterEntity) {
                        break;
                    }
                    MarketingPosterEntity poster = new MarketingPosterEntity();
                    poster.setSend(posterEntity.getSend() + 1);
                    marketingPosterService.update(poster, Wrappers.<MarketingPosterEntity>lambdaQuery().eq(MarketingPosterEntity::getId, idAll));
                    break;
                case 4:
                    MarketingPdfEntity pdfEntity = marketingPdfService.getOne(Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getId, idAll));
                    if (null == pdfEntity) {
                        break;
                    }
                    MarketingPdfEntity pdf = new MarketingPdfEntity();
                    pdf.setSend(pdfEntity.getSend() + 1);
                    marketingPdfService.update(pdf, Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getId, idAll));
                    break;
                case 5:
                    MarketingPptEntity pptEntity = marketingPptService.getOne(Wrappers.<MarketingPptEntity>lambdaQuery().eq(MarketingPptEntity::getId, idAll));
                    if (null == pptEntity) {
                        break;
                    }
                    MarketingPptEntity pptEntity1 = new MarketingPptEntity();
                    pptEntity1.setSend(pptEntity.getSend() + 1);
                    marketingPptService.update(pptEntity1, Wrappers.<MarketingPptEntity>lambdaQuery().eq(MarketingPptEntity::getId, idAll));
                    break;
                case 6:
                    MarketingExcelEntity one = marketingExcelService.getOne(Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getId, idAll));
                    MarketingExcelEntity excelEntity = new MarketingExcelEntity();
                    excelEntity.setSend(one.getSend()+1);
                    marketingExcelService.update(excelEntity,Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getId,idAll));
                    break;
                case 7:
                    MarketingWordEntity wordEntity = marketingWordService.getOne(Wrappers.<MarketingWordEntity>lambdaQuery().eq(MarketingWordEntity::getId, idAll));
                    if (null == wordEntity) {
                        break;
                    }
                    MarketingWordEntity wordEntity1 = new MarketingWordEntity();
                    wordEntity1.setSend(wordEntity.getSend() + 1);
                    marketingWordService.update(wordEntity1, Wrappers.<MarketingWordEntity>lambdaQuery().eq(MarketingWordEntity::getId,idAll));
                    break;
                default:
                    break;
            }
        } else {
            log.info("该素材一天内向同一个客户重复发送，不计入次数！");
        }

    }

    @Override
    public List allList(Integer hotState) {
        Integer corpId = CorpContextHolder.getCorpId();
        MarketingHotEntity one = getOne(Wrappers.<MarketingHotEntity>lambdaQuery()
                .eq(MarketingHotEntity::getCorpId,corpId)
                .orderByDesc(MarketingHotEntity::getId).last("limit 1"));
        if (null==one){
            ArrayList<Object> objects = new ArrayList<>();
            return objects;
        }
        if(hotState==1){
            //查找全部素材通过发送量降序排列后的前20条记录
            List<MarketingHotVO> collect = marketingHotMapper.allListBySend(corpId,one.getHotUuid()).stream().map(entity -> {
                MarketingHotVO marketingHotVo = new MarketingHotVO();
                BeanUtil.copyProperties(entity, marketingHotVo);
                return marketingHotVo;
            }).collect(Collectors.toList());
            //新建一个list
            LinkedList<Object> objects = new LinkedList<>();
            //设置一个自增值作为排序序号
            AtomicReference<Integer> i= new AtomicReference<>(1);
            //遍历前20条记录加入到新的list里，每遍历一条，序号加1。
            collect.forEach(marketingHotVO -> {
                Integer andSet = i.getAndSet(i.get() + 1);
                marketingHotVO.setOrderBy(andSet);
                objects.add(marketingHotVO);
            });
            return objects;

        }
        //查找全部素材通过打开量降序排列后的前20条记录
        List<MarketingHotVO> collect = marketingHotMapper.allListByOpen(corpId,one.getHotUuid()).stream().map(entity -> {
            MarketingHotVO marketingHotVo = new MarketingHotVO();
            BeanUtil.copyProperties(entity, marketingHotVo);
            return marketingHotVo;
        }).collect(Collectors.toList());
        //新建一个list
        LinkedList<Object> objects = new LinkedList<>();
        //设置一个自增值作为排序序号
        AtomicReference<Integer> i= new AtomicReference<>(1);
        //遍历前20条记录加入到新的list里，每遍历一条，序号加1。
        collect.forEach(marketingHotVO -> {
            Integer andSet = i.getAndSet(i.get() + 1);
            marketingHotVO.setOrderBy(andSet);
            objects.add(marketingHotVO);
        });
        return objects;

    }

    @Override
    public List allListWechat(Integer hotState,String title) {
        Integer corpId = CorpContextHolder.getCorpId();
        MarketingHotEntity one = getOne(Wrappers.<MarketingHotEntity>lambdaQuery()
                .eq(MarketingHotEntity::getCorpId,corpId)
                .orderByDesc(MarketingHotEntity::getId).last("limit 1"));
        if(hotState==1){
            //查找全部素材通过发送量降序排列后的前20条记录
            List<MarketingHotVO> collect = marketingHotMapper.allListBySendWechat(corpId,title,one.getHotUuid()).stream().map(entity -> {
                MarketingHotVO marketingHotVo = new MarketingHotVO();
                BeanUtil.copyProperties(entity, marketingHotVo);
                return marketingHotVo;
            }).collect(Collectors.toList());
            //新建一个list
            LinkedList<Object> objects = new LinkedList<>();
            //设置一个自增值作为排序序号
            AtomicReference<Integer> i= new AtomicReference<>(1);
            //遍历前20条记录加入到新的list里，每遍历一条，序号加1。
            collect.forEach(marketingHotVO -> {
                Integer andSet = i.getAndSet(i.get() + 1);
                marketingHotVO.setOrderBy(andSet);
                objects.add(marketingHotVO);
            });
            return objects;

        }
        //查找全部素材通过打开量降序排列后的前20条记录
        List<MarketingHotVO> collect = marketingHotMapper.allListByOpenWechat(corpId,title,one.getHotUuid()).stream().map(entity -> {
            MarketingHotVO marketingHotVo = new MarketingHotVO();
            BeanUtil.copyProperties(entity, marketingHotVo);
            return marketingHotVo;
        }).collect(Collectors.toList());
        //新建一个list
        LinkedList<Object> objects = new LinkedList<>();
        //设置一个自增值作为排序序号
        AtomicReference<Integer> i= new AtomicReference<>(1);
        //遍历前20条记录加入到新的list里，每遍历一条，序号加1。
        collect.forEach(marketingHotVO -> {
            Integer andSet = i.getAndSet(i.get() + 1);
            marketingHotVO.setOrderBy(andSet);
            objects.add(marketingHotVO);
        });
        return objects;
    }

    @Override
    public List getListByType(Integer type, Integer hotState) {
        int corpId = CorpContextHolder.getCorpId();
        MarketingHotEntity one = getOne(Wrappers.<MarketingHotEntity>lambdaQuery()
                .eq(MarketingHotEntity::getCorpId,corpId)
                .orderByDesc(MarketingHotEntity::getId).last("limit 1"));
        if (null==one){
            ArrayList<Object> objects = new ArrayList<>();
            return objects;
        }

        //新建一个list
        LinkedList<Object> list = new LinkedList<>();
        //设置一个自增值作为排序序号
        AtomicReference<Integer> i= new AtomicReference<>(1);
        if(hotState==3){
            //查找对应素材通过发送量降序排列后的前20条记录
            switch (type) {
                case 1:
                    List<MarketingHotVO> collect = marketingArticleService.list(Wrappers.<MarketingArticleEntity>lambdaQuery()
                            .eq(MarketingArticleEntity::getCorpId, corpId)
                            .eq(MarketingArticleEntity::getMarketingState,2)
                            .eq(MarketingArticleEntity::getHotUuid,one.getHotUuid())
                            .orderByDesc(MarketingArticleEntity::getSend).last("limit 20"))
                            .stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    //遍历前20条记录加入到新的list里，每遍历一条，序号加1。
                    collect.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 2:
                    List<MarketingHotVO> collect1 = marketingLinkService.list(Wrappers.<MarketingLinkEntity>lambdaQuery()
                            .eq(MarketingLinkEntity::getCorpId, corpId)
                            .eq(MarketingLinkEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingLinkEntity::getMarketingState,2)
                            .orderByDesc(MarketingLinkEntity::getSend).last("limit 20"))
                            .stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    collect1.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 3:
                    List<MarketingHotVO> collect2 = marketingPosterService.list(Wrappers.<MarketingPosterEntity>lambdaQuery()
                            .eq(MarketingPosterEntity::getCorpId, corpId)
                            .eq(MarketingPosterEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingPosterEntity::getMarketingState,2)
                            .orderByDesc(MarketingPosterEntity::getSend).last("limit 20"))
                            .stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    LinkedList<Object> list2 = new LinkedList<>();
                    AtomicReference<Integer> k= new AtomicReference<>(1);
                    collect2.forEach(marketingHotVO -> {
                        Integer andSet = k.getAndSet(k.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list2.add(marketingHotVO);
                    });
                    return list2;
                case 4:
                    List<MarketingHotVO> collect3 = marketingPdfService.list(Wrappers.<MarketingPdfEntity>lambdaQuery()
                            .eq(MarketingPdfEntity::getCorpId, corpId)
                            .eq(MarketingPdfEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingPdfEntity::getMarketingState,2)
                            .orderByDesc(MarketingPdfEntity::getSend).last("limit 20"))
                            .stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    collect3.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 5:
                    List<MarketingHotVO> collect4 = marketingPptService.list(Wrappers.<MarketingPptEntity>lambdaQuery()
                            .eq(MarketingPptEntity::getCorpId, corpId)
                            .eq(MarketingPptEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingPptEntity::getMarketingState,2)
                            .orderByDesc(MarketingPptEntity::getSend)
                            .last("limit 20"))
                            .stream().map(entity -> {
                                MarketingHotVO marketingHotVo = new MarketingHotVO();
                                BeanUtil.copyProperties(entity, marketingHotVo);
                                return marketingHotVo;
                            }).collect(Collectors.toList());
                    collect4.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 6:
                    List<MarketingHotVO> collect6 = marketingExcelService.list(Wrappers.<MarketingExcelEntity>lambdaQuery()
                            .eq(MarketingExcelEntity::getCorpId, corpId)
                            .eq(MarketingExcelEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingExcelEntity::getMarketingState,2)
                            .orderByDesc(MarketingExcelEntity::getSend)
                            .last("limit 20"))
                            .stream().map(entity -> {
                                MarketingHotVO marketingHotVo = new MarketingHotVO();
                                BeanUtil.copyProperties(entity, marketingHotVo);
                                return marketingHotVo;
                            }).collect(Collectors.toList());
                    collect6.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 7:
                    List<MarketingHotVO> collect5 = marketingWordService.list(Wrappers.<MarketingWordEntity>lambdaQuery()
                            .eq(MarketingWordEntity::getCorpId, corpId)
                            .eq(MarketingWordEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingWordEntity::getMarketingState,2)
                            .orderByDesc(MarketingWordEntity::getSend)
                            .last("limit 20"))
                            .stream().map(entity -> {
                                MarketingHotVO marketingHotVo = new MarketingHotVO();
                                BeanUtil.copyProperties(entity, marketingHotVo);
                                return marketingHotVo;
                            }).collect(Collectors.toList());
                    collect5.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                default:
                    break;
            }
        }
        if(hotState==4){
            //查找对应素材通过打开量降序排列后的前20条记录
            switch (type) {
                case 1:
                    List<MarketingHotVO> collect = marketingArticleService.list(Wrappers.<MarketingArticleEntity>lambdaQuery()
                            .eq(MarketingArticleEntity::getCorpId, corpId)
                            .eq(MarketingArticleEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingArticleEntity::getMarketingState,2)
                            .orderByDesc(MarketingArticleEntity::getOpen)
                            .last("limit 20"))
                            .stream().map(entity -> {
                                MarketingHotVO marketingHotVo = new MarketingHotVO();
                                BeanUtil.copyProperties(entity, marketingHotVo);
                                return marketingHotVo;
                            }).collect(Collectors.toList());
                    //遍历前20条记录加入到新的list里，每遍历一条，序号加1。
                    collect.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 2:
                    List<MarketingHotVO> collect1 = marketingLinkService.list(Wrappers.<MarketingLinkEntity>lambdaQuery()
                            .eq(MarketingLinkEntity::getCorpId, corpId)
                            .eq(MarketingLinkEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingLinkEntity::getMarketingState,2)
                            .orderByDesc(MarketingLinkEntity::getOpen)
                            .last("limit 20"))
                            .stream().map(entity -> {
                                MarketingHotVO marketingHotVo = new MarketingHotVO();
                                BeanUtil.copyProperties(entity, marketingHotVo);
                                return marketingHotVo;
                            }).collect(Collectors.toList());
                    collect1.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 3:
                    List<MarketingHotVO> collect2 = marketingPosterService.list(Wrappers.<MarketingPosterEntity>lambdaQuery()
                            .eq(MarketingPosterEntity::getCorpId, corpId)
                            .eq(MarketingPosterEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingPosterEntity::getMarketingState,2)
                            .orderByDesc(MarketingPosterEntity::getOpen)
                            .last("limit 20"))
                            .stream().map(entity -> {
                                MarketingHotVO marketingHotVo = new MarketingHotVO();
                                BeanUtil.copyProperties(entity, marketingHotVo);
                                return marketingHotVo;
                            }).collect(Collectors.toList());
                    LinkedList<Object> list2 = new LinkedList<>();
                    AtomicReference<Integer> k= new AtomicReference<>(1);
                    collect2.forEach(marketingHotVO -> {
                        Integer andSet = k.getAndSet(k.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list2.add(marketingHotVO);
                    });
                    return list2;
                case 4:
                    List<MarketingHotVO> collect3 = marketingPdfService.list(Wrappers.<MarketingPdfEntity>lambdaQuery()
                            .eq(MarketingPdfEntity::getCorpId, corpId)
                            .eq(MarketingPdfEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingPdfEntity::getMarketingState,2)
                            .orderByDesc(MarketingPdfEntity::getOpen)
                            .last("limit 20"))
                            .stream().map(entity -> {
                                MarketingHotVO marketingHotVo = new MarketingHotVO();
                                BeanUtil.copyProperties(entity, marketingHotVo);
                                return marketingHotVo;
                            }).collect(Collectors.toList());
                    collect3.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 5:
                    List<MarketingHotVO> collect4 = marketingPptService.list(Wrappers.<MarketingPptEntity>lambdaQuery()
                            .eq(MarketingPptEntity::getCorpId, corpId)
                            .eq(MarketingPptEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingPptEntity::getMarketingState,2)
                            .orderByDesc(MarketingPptEntity::getOpen)
                            .last("limit 20"))
                            .stream().map(entity -> {
                                MarketingHotVO marketingHotVo = new MarketingHotVO();
                                BeanUtil.copyProperties(entity, marketingHotVo);
                                return marketingHotVo;
                            }).collect(Collectors.toList());
                    collect4.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 6:
                    List<MarketingHotVO> collect6 = marketingExcelService.list(Wrappers.<MarketingExcelEntity>lambdaQuery()
                            .eq(MarketingExcelEntity::getCorpId, corpId)
                            .eq(MarketingExcelEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingExcelEntity::getMarketingState,2)
                            .orderByDesc(MarketingExcelEntity::getOpen)
                            .last("limit 20"))
                            .stream().map(entity -> {
                                MarketingHotVO marketingHotVo = new MarketingHotVO();
                                BeanUtil.copyProperties(entity, marketingHotVo);
                                return marketingHotVo;
                            }).collect(Collectors.toList());
                    collect6.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 7:
                    List<MarketingHotVO> collect5 = marketingWordService.list(Wrappers.<MarketingWordEntity>lambdaQuery()
                            .eq(MarketingWordEntity::getCorpId, corpId)
                            .eq(MarketingWordEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingWordEntity::getMarketingState,2)
                            .orderByDesc(MarketingWordEntity::getOpen)
                            .last("limit 20"))
                            .stream().map(entity -> {
                                MarketingHotVO marketingHotVo = new MarketingHotVO();
                                BeanUtil.copyProperties(entity, marketingHotVo);
                                return marketingHotVo;
                            }).collect(Collectors.toList());
                    collect5.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                default:
                    return null;
            }
        }
        return null;

    }

    @Override
    public List getListByTypeWechat(Integer type, Integer hotState,String title) {
        Integer corpId = CorpContextHolder.getCorpId();
        MarketingHotEntity one = getOne(Wrappers.<MarketingHotEntity>lambdaQuery()
                .eq(MarketingHotEntity::getCorpId,corpId)
                .orderByDesc(MarketingHotEntity::getId).last("limit 1"));
        //新建一个list
        LinkedList<Object> list = new LinkedList<>();
        //设置一个自增值作为排序序号
        AtomicReference<Integer> i= new AtomicReference<>(1);
        if(hotState==3){
            //查找对应素材通过发送量降序排列后的前20条记录
            switch (type) {
                case 1:
                    List<MarketingHotVO> collect = marketingArticleService.list(Wrappers.<MarketingArticleEntity>lambdaQuery()
                            .eq(MarketingArticleEntity::getCorpId, corpId)
                            .eq(MarketingArticleEntity::getMarketingState,2)
                            .eq(MarketingArticleEntity::getHotUuid,one.getHotUuid())
                            .orderByDesc(MarketingArticleEntity::getSend)
                            .last("limit 20")).stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    //遍历前20条记录加入到新的list里，每遍历一条，序号加1。
                    collect.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 2:
                    List<MarketingHotVO> collect1 = marketingLinkService.list(Wrappers.<MarketingLinkEntity>lambdaQuery()
                            .eq(MarketingLinkEntity::getCorpId, corpId)
                            .eq(MarketingLinkEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingLinkEntity::getMarketingState,2)
                            .orderByDesc(MarketingLinkEntity::getSend)
                            .last("limit 20")).stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    collect1.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 3:
                    List<MarketingHotVO> collect2 = marketingPosterService.list(Wrappers.<MarketingPosterEntity>lambdaQuery()
                            .eq(MarketingPosterEntity::getCorpId, corpId)
                            .eq(MarketingPosterEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingPosterEntity::getMarketingState,2)
                            .orderByDesc(MarketingPosterEntity::getSend)
                            .last("limit 20")).stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    LinkedList<Object> list2 = new LinkedList<>();
                    AtomicReference<Integer> k= new AtomicReference<>(1);
                    collect2.forEach(marketingHotVO -> {
                        Integer andSet = k.getAndSet(k.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list2.add(marketingHotVO);
                    });
                    return list2;
                case 4:
                    List<MarketingHotVO> collect3 = marketingPdfService.list(Wrappers.<MarketingPdfEntity>lambdaQuery()
                            .eq(MarketingPdfEntity::getCorpId, corpId)
                            .eq(MarketingPdfEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingPdfEntity::getMarketingState,2)
                            .orderByDesc(MarketingPdfEntity::getSend)
                            .last("limit 20")).stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    collect3.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 5:
                    List<MarketingHotVO> collect4 = marketingPptService.list(Wrappers.<MarketingPptEntity>lambdaQuery()
                            .eq(MarketingPptEntity::getCorpId, corpId)
                            .eq(MarketingPptEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingPptEntity::getMarketingState,2)
                            .orderByDesc(MarketingPptEntity::getSend)
                            .last("limit 20"))
                            .stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    collect4.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 6:
                    List<MarketingHotVO> collect6 = marketingExcelService.list(Wrappers.<MarketingExcelEntity>lambdaQuery()
                            .eq(MarketingExcelEntity::getCorpId, corpId)
                            .eq(MarketingExcelEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingExcelEntity::getMarketingState,2)
                            .orderByDesc(MarketingExcelEntity::getSend)
                            .last("limit 20"))
                            .stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    collect6.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 7:
                    List<MarketingHotVO> collect5 = marketingWordService.list(Wrappers.<MarketingWordEntity>lambdaQuery()
                            .eq(MarketingWordEntity::getCorpId, corpId)
                            .eq(MarketingWordEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingWordEntity::getMarketingState,2)
                            .orderByDesc(MarketingWordEntity::getSend)
                            .last("limit 20"))
                            .stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    collect5.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                default:
                    break;
            }
        }
        if(hotState==4){
            //查找对应素材通过打开量降序排列后的前20条记录
            switch (type) {
                case 1:
                    List<MarketingHotVO> collect = marketingArticleService.list(Wrappers.<MarketingArticleEntity>lambdaQuery()
                            .eq(MarketingArticleEntity::getCorpId, corpId)
                            .eq(MarketingArticleEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingArticleEntity::getMarketingState,2)
                            .orderByDesc(MarketingArticleEntity::getOpen)
                            .last("limit 20"))
                            .stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    //遍历前20条记录加入到新的list里，每遍历一条，序号加1。
                    collect.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 2:
                    List<MarketingHotVO> collect1 = marketingLinkService.list(Wrappers.<MarketingLinkEntity>lambdaQuery()
                            .eq(MarketingLinkEntity::getCorpId, corpId)
                            .eq(MarketingLinkEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingLinkEntity::getMarketingState,2)
                            .orderByDesc(MarketingLinkEntity::getOpen)
                            .last("limit 20"))
                            .stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    collect1.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 3:
                    List<MarketingHotVO> collect2 = marketingPosterService.list(Wrappers.<MarketingPosterEntity>lambdaQuery()
                            .eq(MarketingPosterEntity::getCorpId, corpId)
                            .eq(MarketingPosterEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingPosterEntity::getMarketingState,2)
                            .orderByDesc(MarketingPosterEntity::getOpen)
                            .last("limit 20"))
                            .stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    LinkedList<Object> list2 = new LinkedList<>();
                    AtomicReference<Integer> k= new AtomicReference<>(1);
                    collect2.forEach(marketingHotVO -> {
                        Integer andSet = k.getAndSet(k.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list2.add(marketingHotVO);
                    });
                    return list2;
                case 4:
                    List<MarketingHotVO> collect3 = marketingPdfService.list(Wrappers.<MarketingPdfEntity>lambdaQuery()
                            .eq(MarketingPdfEntity::getCorpId, corpId)
                            .eq(MarketingPdfEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingPdfEntity::getMarketingState,2)
                            .orderByDesc(MarketingPdfEntity::getOpen)
                            .last("limit 20"))
                            .stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    collect3.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 5:
                    List<MarketingHotVO> collect4 = marketingPptService.list(Wrappers.<MarketingPptEntity>lambdaQuery()
                            .eq(MarketingPptEntity::getCorpId, corpId)
                            .eq(MarketingPptEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingPptEntity::getMarketingState,2)
                            .orderByDesc(MarketingPptEntity::getOpen)
                            .last("limit 20"))
                            .stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    collect4.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 6:
                    List<MarketingHotVO> collect6 = marketingExcelService.list(Wrappers.<MarketingExcelEntity>lambdaQuery()
                            .eq(MarketingExcelEntity::getCorpId, corpId)
                            .eq(MarketingExcelEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingExcelEntity::getMarketingState,2)
                            .orderByDesc(MarketingExcelEntity::getOpen)
                            .last("limit 20"))
                            .stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    collect6.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                case 7:
                    List<MarketingHotVO> collect5 = marketingWordService.list(Wrappers.<MarketingWordEntity>lambdaQuery()
                            .eq(MarketingWordEntity::getCorpId, corpId)
                            .eq(MarketingWordEntity::getHotUuid,one.getHotUuid())
                            .eq(MarketingWordEntity::getMarketingState,2)
                            .orderByDesc(MarketingWordEntity::getOpen)
                            .last("limit 20"))
                            .stream().map(entity -> {
                        MarketingHotVO marketingHotVo = new MarketingHotVO();
                        BeanUtil.copyProperties(entity, marketingHotVo);
                        return marketingHotVo;
                    }).collect(Collectors.toList());
                    collect5.forEach(marketingHotVO -> {
                        Integer andSet = i.getAndSet(i.get() + 1);
                        marketingHotVO.setOrderBy(andSet);
                        list.add(marketingHotVO);
                    });
                    return list;
                default:
                    return null;
            }
        }
        return null;
    }

    @Override
    public void saveAndRelease(Integer hotState) {
        Integer corpId = CorpContextHolder.getCorpId();
        //记录发布方式
        MarketingHotEntity marketingHotEntity = new MarketingHotEntity();
        marketingHotEntity.setHotState(hotState);
        marketingHotEntity.setHotUuid( IdUtil.objectId());
        marketingHotEntity.setCorpId(corpId);
        boolean save = save(marketingHotEntity);
        if(save){
            MarketingHotEntity one = getOne(Wrappers.<MarketingHotEntity>lambdaQuery()
                    .eq(MarketingHotEntity::getCorpId,corpId)
                    .orderByDesc(MarketingHotEntity::getId)
                    .last("limit 1"));
            //根据接受的发布方式选择对应的发布代码。
            if(hotState==1 || hotState==2){
                //获取20条全部素材的记录，1为发送量排序，2为打开量排序
                List<MarketingHotDTO> hots = change(hotState);
                hots.forEach(marketingHotDTO -> {
                            Integer types = marketingHotDTO.getType();
                            Integer id = marketingHotDTO.getId();
                            //根据素材类型选择发布素材
                            switch (types) {
                                case 1:
                                    MarketingArticleEntity articleEntity = new MarketingArticleEntity();
                                    articleEntity.setId(id);
                                    articleEntity.setHotState(hotState);
                                    articleEntity.setHotUuid(one.getHotUuid());
                                    marketingArticleService.update(articleEntity, Wrappers.<MarketingArticleEntity>lambdaQuery()
                                            .eq(MarketingArticleEntity::getCorpId,corpId)
                                            .eq(MarketingArticleEntity::getId,id));
                                    break;
                                case 2:
                                    MarketingLinkEntity linkEntity = new MarketingLinkEntity();
                                    linkEntity.setId(id);
                                    linkEntity.setHotState(hotState);
                                    linkEntity.setHotUuid(one.getHotUuid());
                                    marketingLinkService.update(linkEntity, Wrappers.<MarketingLinkEntity>lambdaQuery()
                                            .eq(MarketingLinkEntity::getCorpId,corpId)
                                            .eq(MarketingLinkEntity::getId,id));
                                    break;
                                case 3:
                                    MarketingPosterEntity posterEntity = new MarketingPosterEntity();
                                    posterEntity.setId(id);
                                    posterEntity.setHotState(hotState);
                                    posterEntity.setHotUuid(one.getHotUuid());
                                    marketingPosterService.update(posterEntity, Wrappers.<MarketingPosterEntity>lambdaQuery()
                                            .eq(MarketingPosterEntity::getCorpId,corpId)
                                            .eq(MarketingPosterEntity::getId,id));
                                    break;
                                case 4:
                                    MarketingPdfEntity pdfEntity = new MarketingPdfEntity();
                                    pdfEntity.setId(id);
                                    pdfEntity.setHotState(hotState);
                                    pdfEntity.setHotUuid(one.getHotUuid());
                                    marketingPdfService.update(pdfEntity, Wrappers.<MarketingPdfEntity>lambdaQuery()
                                            .eq(MarketingPdfEntity::getCorpId,corpId)
                                            .eq(MarketingPdfEntity::getId,id));
                                    break;
                                case 5:
                                    MarketingPptEntity pptEntity = new MarketingPptEntity();
                                    pptEntity.setId(id);
                                    pptEntity.setHotState(hotState);
                                    pptEntity.setHotUuid(one.getHotUuid());
                                    marketingPptService.update(pptEntity, Wrappers.<MarketingPptEntity>lambdaQuery()
                                            .eq(MarketingPptEntity::getCorpId,corpId)
                                            .eq(MarketingPptEntity::getId,id));
                                    break;
                                case 6:
                                    MarketingExcelEntity excelEntity = new MarketingExcelEntity();
                                    excelEntity.setId(id);
                                    excelEntity.setHotState(hotState);
                                    excelEntity.setHotUuid(one.getHotUuid());
                                    marketingExcelService.update(excelEntity, Wrappers.<MarketingExcelEntity>lambdaQuery()
                                            .eq(MarketingExcelEntity::getCorpId,corpId)
                                            .eq(MarketingExcelEntity::getId,id));
                                    break;
                                case 7:
                                    MarketingWordEntity wordEntity = new MarketingWordEntity();
                                    wordEntity.setId(id);
                                    wordEntity.setHotState(hotState);
                                    wordEntity.setHotUuid(one.getHotUuid());
                                    marketingWordService.update(wordEntity, Wrappers.<MarketingWordEntity>lambdaQuery()
                                            .eq(MarketingWordEntity::getCorpId,corpId)
                                            .eq(MarketingWordEntity::getId,id));
                                    break;
                                default:
                                    break;
                            }
                        }
                );
            }
            if (hotState == 3) {
                List<MarketingHotDTO> collect = marketingArticleService.list(Wrappers.<MarketingArticleEntity>lambdaQuery()
                        .eq(MarketingArticleEntity::getCorpId, corpId)
                        .eq(MarketingArticleEntity::getMarketingState,2)
                        .orderByDesc(MarketingArticleEntity::getSend)
                        .last("limit 20")).stream().map(entity -> {
                    MarketingHotDTO marketingHotDTO = new MarketingHotDTO();
                    BeanUtil.copyProperties(entity, marketingHotDTO);
                    return marketingHotDTO;
                }).collect(Collectors.toList());
                collect.forEach(entity -> {
                    Integer id = entity.getId();
                    MarketingArticleEntity articleEntity = new MarketingArticleEntity();
                    articleEntity.setId(id);
                    articleEntity.setHotState(hotState);
                    articleEntity.setHotUuid(one.getHotUuid());
                    marketingArticleService.update(articleEntity, Wrappers.<MarketingArticleEntity>lambdaQuery().eq(MarketingArticleEntity::getId,id));
                });
                List<MarketingHotDTO> list = marketingLinkService.list(Wrappers.<MarketingLinkEntity>lambdaQuery()
                        .eq(MarketingLinkEntity::getCorpId, corpId)
                        .eq(MarketingLinkEntity::getMarketingState,2)
                        .orderByDesc(MarketingLinkEntity::getSend)
                        .last("limit 20")).stream().map(entity -> {
                    MarketingHotDTO marketingHotDTO = new MarketingHotDTO();
                    BeanUtil.copyProperties(entity, marketingHotDTO);
                    return marketingHotDTO;
                }).collect(Collectors.toList());
                list.forEach(marketingHotDTO -> {
                    Integer id = marketingHotDTO.getId();
                    MarketingLinkEntity linkEntity = new MarketingLinkEntity();
                    linkEntity.setId(id);
                    linkEntity.setHotState(hotState);
                    linkEntity.setHotUuid(one.getHotUuid());
                    marketingLinkService.update(linkEntity, Wrappers.<MarketingLinkEntity>lambdaQuery()
                            .eq(MarketingLinkEntity::getId,id));
                });
                List<MarketingHotDTO> poster = marketingPosterService.list(Wrappers.<MarketingPosterEntity>lambdaQuery()
                        .eq(MarketingPosterEntity::getCorpId, corpId)
                        .eq(MarketingPosterEntity::getMarketingState,2)
                        .orderByDesc(MarketingPosterEntity::getSend)
                        .last("limit 20")).stream().map(entity -> {
                    MarketingHotDTO marketingHotDTO = new MarketingHotDTO();
                    BeanUtil.copyProperties(entity, marketingHotDTO);
                    return marketingHotDTO;
                }).collect(Collectors.toList());
                poster.forEach(marketingHotDTO -> {
                    Integer id = marketingHotDTO.getId();
                    MarketingPosterEntity posterEntity = new MarketingPosterEntity();
                    posterEntity.setId(id);
                    posterEntity.setHotState(hotState);
                    posterEntity.setHotUuid(one.getHotUuid());
                    marketingPosterService.update(posterEntity, Wrappers.<MarketingPosterEntity>lambdaQuery()
                            .eq(MarketingPosterEntity::getId,id));
                });
                List<MarketingHotDTO> pdf = marketingPdfService.list(Wrappers.<MarketingPdfEntity>lambdaQuery()
                        .eq(MarketingPdfEntity::getCorpId, corpId)
                        .eq(MarketingPdfEntity::getMarketingState,2)
                        .orderByDesc(MarketingPdfEntity::getSend)
                        .last("limit 20")).stream().map(entity -> {
                    MarketingHotDTO marketingHotDTO = new MarketingHotDTO();
                    BeanUtil.copyProperties(entity, marketingHotDTO);
                    return marketingHotDTO;
                }).collect(Collectors.toList());
                pdf.forEach(marketingHotDTO -> {
                    Integer id = marketingHotDTO.getId();
                    MarketingPdfEntity pdfEntity = new MarketingPdfEntity();
                    pdfEntity.setId(id);
                    pdfEntity.setHotState(hotState);
                    pdfEntity.setHotUuid(one.getHotUuid());
                    marketingPdfService.update(pdfEntity, Wrappers.<MarketingPdfEntity>lambdaQuery()
                            .eq(MarketingPdfEntity::getId,id));
                });
                List<MarketingHotDTO> ppt = marketingPptService.list(Wrappers.<MarketingPptEntity>lambdaQuery()
                        .eq(MarketingPptEntity::getCorpId, corpId)
                        .eq(MarketingPptEntity::getMarketingState,2)
                        .orderByDesc(MarketingPptEntity::getSend)
                        .last("limit 20")).stream().map(entity -> {
                    MarketingHotDTO marketingHotDTO = new MarketingHotDTO();
                    BeanUtil.copyProperties(entity, marketingHotDTO);
                    return marketingHotDTO;
                }).collect(Collectors.toList());
                ppt.forEach(marketingHotDTO -> {
                    Integer id = marketingHotDTO.getId();
                    MarketingPptEntity pptEntity = new MarketingPptEntity();
                    pptEntity.setId(id);
                    pptEntity.setHotState(hotState);
                    pptEntity.setHotUuid(one.getHotUuid());
                    marketingPptService.update(pptEntity,  Wrappers.<MarketingPptEntity>lambdaQuery()
                            .eq(MarketingPptEntity::getId,id));
                });
                List<MarketingHotDTO> word = marketingWordService.list(Wrappers.<MarketingWordEntity>lambdaQuery()
                        .eq(MarketingWordEntity::getCorpId, corpId)
                        .eq(MarketingWordEntity::getMarketingState,2)
                        .orderByDesc(MarketingWordEntity::getSend)
                        .last("limit 20")).stream().map(entity -> {
                    MarketingHotDTO marketingHotDTO = new MarketingHotDTO();
                    BeanUtil.copyProperties(entity, marketingHotDTO);
                    return marketingHotDTO;
                }).collect(Collectors.toList());
                word.forEach(marketingHotDTO -> {
                    Integer id = marketingHotDTO.getId();
                    MarketingWordEntity wordEntity = new MarketingWordEntity();
                    wordEntity.setId(id);
                    wordEntity.setHotState(hotState);
                    wordEntity.setHotUuid(one.getHotUuid());
                    marketingWordService.update(wordEntity, Wrappers.<MarketingWordEntity>lambdaQuery().eq(MarketingWordEntity::getId,id));
                });
                List<MarketingHotDTO> excel = marketingExcelService.list(Wrappers.<MarketingExcelEntity>lambdaQuery()
                        .eq(MarketingExcelEntity::getCorpId, corpId)
                        .eq(MarketingExcelEntity::getMarketingState,2)
                        .orderByDesc(MarketingExcelEntity::getSend)
                        .last("limit 20")).stream().map(entity -> {
                    MarketingHotDTO marketingHotDTO = new MarketingHotDTO();
                    BeanUtil.copyProperties(entity, marketingHotDTO);
                    return marketingHotDTO;
                }).collect(Collectors.toList());
                excel.forEach(marketingHotDTO -> {
                    Integer id = marketingHotDTO.getId();
                    MarketingExcelEntity excelEntity = new MarketingExcelEntity();
                    excelEntity.setId(id);
                    excelEntity.setHotState(hotState);
                    excelEntity.setHotUuid(one.getHotUuid());
                    marketingExcelService.update(excelEntity,  Wrappers.<MarketingExcelEntity>lambdaQuery()
                            .eq(MarketingExcelEntity::getId,id));
                });
            }else if (hotState == 4) {
                List<MarketingHotDTO> collect = marketingArticleService.list(Wrappers.<MarketingArticleEntity>lambdaQuery()
                        .eq(MarketingArticleEntity::getCorpId, corpId)
                        .eq(MarketingArticleEntity::getMarketingState,2)
                        .orderByDesc(MarketingArticleEntity::getOpen)
                        .last("limit 20")).stream().map(entity -> {
                    MarketingHotDTO marketingHotDTO = new MarketingHotDTO();
                    BeanUtil.copyProperties(entity, marketingHotDTO);
                    return marketingHotDTO;
                }).collect(Collectors.toList());
                collect.forEach(entity -> {
                    Integer id = entity.getId();
                    MarketingArticleEntity articleEntity = new MarketingArticleEntity();
                    articleEntity.setId(id);
                    articleEntity.setHotState(hotState);
                    articleEntity.setHotUuid(one.getHotUuid());
                    marketingArticleService.update(articleEntity, Wrappers.<MarketingArticleEntity>lambdaQuery().eq(MarketingArticleEntity::getId,id));
                });
                List<MarketingHotDTO> list = marketingLinkService.list(Wrappers.<MarketingLinkEntity>lambdaQuery()
                        .eq(MarketingLinkEntity::getCorpId, corpId)
                        .eq(MarketingLinkEntity::getMarketingState,2)
                        .orderByDesc(MarketingLinkEntity::getOpen)
                        .last("limit 20")).stream().map(entity -> {
                    MarketingHotDTO marketingHotDTO = new MarketingHotDTO();
                    BeanUtil.copyProperties(entity, marketingHotDTO);
                    return marketingHotDTO;
                }).collect(Collectors.toList());
                list.forEach(marketingHotDTO -> {
                    Integer id = marketingHotDTO.getId();
                    MarketingLinkEntity linkEntity = new MarketingLinkEntity();
                    linkEntity.setId(id);
                    linkEntity.setHotState(hotState);
                    linkEntity.setHotUuid(one.getHotUuid());
                    marketingLinkService.update(linkEntity,  Wrappers.<MarketingLinkEntity>lambdaQuery().eq(MarketingLinkEntity::getId,id));
                });
                List<MarketingHotDTO> poster = marketingPosterService.list(Wrappers.<MarketingPosterEntity>lambdaQuery()
                        .eq(MarketingPosterEntity::getCorpId, corpId)
                        .eq(MarketingPosterEntity::getMarketingState,2)
                        .orderByDesc(MarketingPosterEntity::getOpen)
                        .last("limit 20")).stream().map(entity -> {
                    MarketingHotDTO marketingHotDTO = new MarketingHotDTO();
                    BeanUtil.copyProperties(entity, marketingHotDTO);
                    return marketingHotDTO;
                }).collect(Collectors.toList());
                poster.forEach(marketingHotDTO -> {
                    Integer id = marketingHotDTO.getId();
                    MarketingPosterEntity posterEntity = new MarketingPosterEntity();
                    posterEntity.setId(id);
                    posterEntity.setHotState(hotState);
                    posterEntity.setHotUuid(one.getHotUuid());
                    marketingPosterService.update(posterEntity,  Wrappers.<MarketingPosterEntity>lambdaQuery().eq(MarketingPosterEntity::getId,id));
                });
                List<MarketingHotDTO> pdf = marketingPdfService.list(Wrappers.<MarketingPdfEntity>lambdaQuery()
                        .eq(MarketingPdfEntity::getCorpId, corpId)
                        .eq(MarketingPdfEntity::getMarketingState,2)
                        .orderByDesc(MarketingPdfEntity::getOpen)
                        .last("limit 20")).stream().map(entity -> {
                    MarketingHotDTO marketingHotDTO = new MarketingHotDTO();
                    BeanUtil.copyProperties(entity, marketingHotDTO);
                    return marketingHotDTO;
                }).collect(Collectors.toList());
                pdf.forEach(marketingHotDTO -> {
                    Integer id = marketingHotDTO.getId();
                    MarketingPdfEntity pdfEntity = new MarketingPdfEntity();
                    pdfEntity.setId(id);
                    pdfEntity.setHotState(hotState);
                    pdfEntity.setHotUuid(one.getHotUuid());
                    marketingPdfService.update(pdfEntity,  Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getId,id));
                });
                List<MarketingHotDTO> ppt = marketingPptService.list(Wrappers.<MarketingPptEntity>lambdaQuery()
                        .eq(MarketingPptEntity::getCorpId, corpId)
                        .eq(MarketingPptEntity::getMarketingState,2)
                        .orderByDesc(MarketingPptEntity::getOpen)
                        .last("limit 20")).stream().map(entity -> {
                    MarketingHotDTO marketingHotDTO = new MarketingHotDTO();
                    BeanUtil.copyProperties(entity, marketingHotDTO);
                    return marketingHotDTO;
                }).collect(Collectors.toList());
                ppt.forEach(marketingHotDTO -> {
                    Integer id = marketingHotDTO.getId();
                    MarketingPptEntity pptEntity = new MarketingPptEntity();
                    pptEntity.setId(id);
                    pptEntity.setHotState(hotState);
                    pptEntity.setHotUuid(one.getHotUuid());
                    marketingPptService.update(pptEntity, Wrappers.<MarketingPptEntity>lambdaQuery().eq(MarketingPptEntity::getId,id));
                });
                List<MarketingHotDTO> word = marketingWordService.list(Wrappers.<MarketingWordEntity>lambdaQuery()
                        .eq(MarketingWordEntity::getCorpId, corpId)
                        .eq(MarketingWordEntity::getMarketingState,2)
                        .orderByDesc(MarketingWordEntity::getOpen)
                        .last("limit 20")).stream().map(entity -> {
                    MarketingHotDTO marketingHotDTO = new MarketingHotDTO();
                    BeanUtil.copyProperties(entity, marketingHotDTO);
                    return marketingHotDTO;
                }).collect(Collectors.toList());
                word.forEach(marketingHotDTO -> {
                    Integer id = marketingHotDTO.getId();
                    MarketingWordEntity wordEntity = new MarketingWordEntity();
                    wordEntity.setId(id);
                    wordEntity.setHotState(hotState);
                    wordEntity.setHotUuid(one.getHotUuid());
                    marketingWordService.update(wordEntity, Wrappers.<MarketingWordEntity>lambdaQuery().eq(MarketingWordEntity::getId,id));
                });
                List<MarketingHotDTO> excel = marketingExcelService.list(Wrappers.<MarketingExcelEntity>lambdaQuery()
                        .eq(MarketingExcelEntity::getCorpId, corpId)
                        .eq(MarketingExcelEntity::getMarketingState,2)
                        .orderByDesc(MarketingExcelEntity::getOpen)
                        .last("limit 20")).stream().map(entity -> {
                    MarketingHotDTO marketingHotDTO = new MarketingHotDTO();
                    BeanUtil.copyProperties(entity, marketingHotDTO);
                    return marketingHotDTO;
                }).collect(Collectors.toList());
                excel.forEach(marketingHotDTO -> {
                    Integer id = marketingHotDTO.getId();
                    MarketingExcelEntity excelEntity = new MarketingExcelEntity();
                    excelEntity.setId(id);
                    excelEntity.setHotState(hotState);
                    excelEntity.setHotUuid(one.getHotUuid());
                    marketingExcelService.update(excelEntity,  Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getId,id));
                });
            }
        }
    }

    @Override
    public Map<String,Integer> hotState() {
        MarketingHotEntity one = getOne(Wrappers.<MarketingHotEntity>lambdaQuery().eq(MarketingHotEntity::getCorpId,CorpContextHolder.getCorpId()).orderByDesc(MarketingHotEntity::getId).last("limit 1"));
        if(null==one){
           throw new CheckedException("热榜未发布！");
        }
        HashMap<String, Integer> hashMap = new HashMap<>(1);
        hashMap.put("hotState",one.getHotState());
        return hashMap;
    }


    public List<MarketingHotDTO> change(Integer hotState){
        Integer corpId = CorpContextHolder.getCorpId();
        switch (hotState){
            case 1: List<MarketingHotDTO> hots = marketingHotMapper.listByChooseBySend(corpId).stream().map(entity -> {
                MarketingHotDTO marketingHotDTO = new MarketingHotDTO();
                BeanUtil.copyProperties(entity, marketingHotDTO);
                return marketingHotDTO;
            }).collect(Collectors.toList());
                return hots;
            case 2:List<MarketingHotDTO> hot2 = marketingHotMapper.listByChooseByOpen(corpId).stream().map(entity -> {
                MarketingHotDTO marketingHotDTO = new MarketingHotDTO();
                BeanUtil.copyProperties(entity, marketingHotDTO);
                return marketingHotDTO;
            }).collect(Collectors.toList());
                return hot2;
            default:
                return null;
        }
    }
      public Integer redisCuntOpen(String id,String type){

          Integer o =(Integer) RedisUtil.get(id + type);
          if(null==o){
              Integer i=0;
              RedisUtil.set(id + type,i,60*10);
          }else {
              o++;
              RedisUtil.set(id + type,o);
          }
          return o;
      }
}

